package com.nwpu.coursesupport.service.imple;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.AssistantCourseSearchCriteria;
import com.nwpu.coursesupport.criteria.AssistantSearchCriteria;
import com.nwpu.coursesupport.entity.Assistant;
import com.nwpu.coursesupport.entity.AssistantCourse;
import com.nwpu.coursesupport.entity.Course;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.mapper.AssistantCourseMapper;
import com.nwpu.coursesupport.mapper.AssistantMapper;
import com.nwpu.coursesupport.mapper.CourseMapper;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.IDListParam;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.tool.UpdateUtil;
import com.nwpu.coursesupport.view.AssistantSearchView;
import com.nwpu.coursesupport.view.CourseView;

@Service
public class AssistantCourseService extends AbstractService{
    @Autowired
    private AssistantCourseMapper assistantCourseMapper;
    @Autowired
    private AssistantMapper assistantMapper;
    @Autowired
    private CourseMapper courseMapper;
    
    protected static Logger logger = LoggerFactory.getLogger(AssistantCourseService.class);

    public JSONResult getAssistantByCid(IDParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() <= 0){
        	logger.error("getAssistantByCid:参数有误");
            rst.fail("参数有误");
            return rst;
        }
        
        Course course=this.courseMapper.findById(p.getId());
        if(course==null){
        	logger.error("该课程不存在");
        	return rst.fail("该课程不存在");
        }
        List<Assistant> list=this.assistantCourseMapper.getAssistantByCourseId(p.getId());
        
        rst.addData("Assistants", list);
        rst.addData("course", course);
        rst.success();
        return rst;
    }
    public JSONResult getCourseByAid(IDParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }
        
        Assistant assistant=this.assistantMapper.findById(p.getId());
        if(assistant==null){
        	logger.error("该助教不存在");
        	return rst.fail("该助教不存在");
        }
        List<Course> list=this.assistantCourseMapper.getCouseByAssistantId(p.getId());
        
        rst.addData("courses", list);
        rst.addData("assistant", assistant);
        rst.success();
        return rst;
    }
    
    public JSONResult getCourseViewByAid(IDParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }
        
        Assistant assistant=this.assistantMapper.findById(p.getId());
        if(assistant==null){
        	logger.error("该助教不存在");
        	return rst.fail("该助教不存在");
        }
        AssistantCourseSearchCriteria sc=new AssistantCourseSearchCriteria();
        sc.setAssistantId(p.getId());
        List<CourseView> list=this.assistantCourseMapper.getCouseViewByAssistantId(sc);
        rst.addData("list", list);
        rst.addData("assistant", assistant);
        rst.addData("pageNo", 1);
        rst.addData("allpages", 1);
        rst.success();
        return rst;
    }
    
    public JSONResult detail(IDParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }
        
        AssistantCourse assistantCourse = this.assistantCourseMapper.findById(p.getId());
        
        rst.addData("assistantCourse", assistantCourse);
        rst.success();
        return rst;
    }

    public JSONResult listData(AssistantCourseSearchCriteria sc){
    	 JSONResult rst = new JSONResult();
         rst.fail();
         if(sc==null || sc.getKeywords()==null){
        	 logger.error("参数有误");
        	 return rst.fail("参数有误");
         }
         AssistantSearchCriteria asc=new AssistantSearchCriteria();
         asc.setKeywords(sc.getKeywords());
         List<Assistant> assistants=this.assistantMapper.find(asc);
         List<AssistantSearchView> searchView=new ArrayList<AssistantSearchView>();
         if(sc.getCourseId()<=0){
        	 assistants.forEach(item->{
            	 AssistantSearchView asv=new AssistantSearchView();
        		 UpdateUtil.setValues(asv,item);
        		 searchView.add(asv);
    		});
        	 rst.addData("list",searchView);
        	 
         }
         List<Assistant> list=this.assistantCourseMapper.getAssistantByCourseId(sc.getCourseId());
         
        assistants.forEach(item->{
        	 AssistantSearchView asv=new AssistantSearchView();
    		 UpdateUtil.setValues(asv,item);
    		 searchView.add(asv);
		    if(list.contains(item)){
		       asv.setHasJoined(1);
		    }else{
		    	asv.setHasJoined(0);
		    }
		});
         rst.addData("list",searchView);
         rst.success();
         return rst;
    }

    public JSONResult create(AssistantCourse assistantCourse) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (assistantCourse == null){
        	logger.error("参数有误");
            return rst.fail("参数有误");
        }
        Assistant assistant=this.assistantMapper.findById(assistantCourse.getAssistantId());
        if(assistant==null){
        	logger.error("该助教不存在");
        	return rst.fail("该助教不存在");
        }
       
        Course course=this.courseMapper.findById(assistantCourse.getCourseId());
        if(course==null){
        	logger.error("该课程不存在");
        	return rst.fail("该课程不存在");
        }
        
        AssistantCourseSearchCriteria sc=new AssistantCourseSearchCriteria();
        sc.setAssistantId(assistantCourse.getAssistantId());
        sc.setCourseId(assistantCourse.getCourseId());
        
        
        List<AssistantCourse> list=this.assistantCourseMapper.find(sc);
        if(list!=null&&list.size()>0){
        	logger.error("该助教已在对应的课程中");
        	return rst.fail("该助教已在对应的课程中");
        }
        Date date = new Date();
        if(assistantCourse.getJoinDate() == null){
        	assistantCourse.setJoinDate(date);
        }
        if (!this.assistantCourseMapper.save(assistantCourse)){
        	logger.error("保存失败");
            return rst.fail("保存失败");
        }
        
        rst.addData("assistantCourse", assistantCourse);
        rst.success();
        return rst;
    }

    public JSONResult remove(IDParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getId() <= 0){
        	logger.error("参数有误");
            rst.fail("参数有误");
            return rst;
        }

        AssistantCourse assistantCourse = this.assistantCourseMapper.findById(p.getId());
        if (assistantCourse == null){
        	logger.error("记录不存在或已被删除");
            return rst.fail("记录不存在或已被删除");
        }

        if (!this.assistantCourseMapper.delete(p.getId())){
        	logger.error("删除失败");
            return rst.fail("删除失败");
        }
        rst.addData("assistantCourse", assistantCourse);
        rst.success();
        return rst;
    }

    public JSONResult removeAll(IDListParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getIds() == null ||p.getKeyId()==null|| p.getIds().isEmpty()){
        	logger.error("参数有误");
        	rst.fail("参数有误");
            return rst;
        }
       
        this.assistantCourseMapper.batchDelete(p);
       
        rst.success();
        return rst;
    }



}
